package com.cloudera;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.io.IntWritable;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by shuyun on 2016/9/28.
 */
public class UserAge extends UDF {
    /**
     * 计算岁数
     * @param birthYear
     * @return
     * @throws UDFArgumentException
     */
    public IntWritable evaluate(IntWritable birthYear) throws UDFArgumentException {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());

        int currentYear = c.get(Calendar.YEAR);
        if(null == birthYear || birthYear.get() == 0 || currentYear < birthYear.get())
            return new IntWritable(-1);

        return new IntWritable((currentYear - birthYear.get()) + 1);
    }
}
